$(function () {
    $(".id").focus();
    let inputUtil = new Input_util();
    let administrator = new Administrator();
    $(".button-login").on("click", function () {
        login();
    })

    $(document).keydown(function(event) {
        // 检测按下的键是否为回车键（keyCode为13）
        if (event.keyCode === 13) {
            // 在此处执行回车事件的操作
            login()
        }
    });

    function login(){
        let id = $(".id").val();
        let password = $(".password").val();
        //判断是否为空
        let isIdNull = inputUtil.isEmail(id);
        let isPasswordNull = inputUtil.isEmail(password);
        //判断Id是否是账号格式
        let isIdFormat = inputUtil.validateMinLength(id, 4);
        //判断password是否是密码格式
        let isPasswordFormat = inputUtil.validateRangeLength(password,
            6, 24);

        let hintText = $(".hint-text")
        if (isIdNull === false && isPasswordNull === false
            && isIdFormat === true && isPasswordFormat === true) {
            administrator.judgementMenu(id, password).then(function (response) {
                console.log(response)
                // 在这里获取后端返回的响应数据
                if (response === ""){
                    //跳转主页
                    window.location.href = "/www.xiaomi.com/background/home"
                }else {
                    hintText[0].innerText = "账号或密码错误！"
                }
            }).catch(function (error) {
                console.error(error);
                window.location.href = "/www.xiaomi.com/error"
            });
        } else {
            hintText[0].innerText = "账号或密码错误！"
        }
    }
    // 获取视频元素和捕获按钮的引用
    let videoElement = $('#videoElement')[0];
    let captureButton = $('.face-login');
    let capturedImage = $('#capturedImage');

    // 将摄像头与视频元素关联
    navigator.mediaDevices.getUserMedia({ video: true })
        .then(function(stream) {
            videoElement.srcObject = stream;
        })
        .catch(function(error) {
            console.error('无法访问摄像头：', error);
        });

    // 添加捕获按钮的点击事件处理程序
    captureButton.click(function() {
        let canvas = document.createElement("canvas");
        let context = canvas.getContext("2d");
        context.drawImage(videoElement, 0, 0, canvas.width, canvas.height);

        canvas.toBlob(function(blob) {
            let file = new File([blob], "filename.jpg", {type: "image/jpeg"});

            let formData = new FormData();
            formData.append("file", file);

            console.log(file)

        }, "image/jpeg");
    });
})